//
//  SearchResultViewController.m
//  jipin
//
//  Created by lcb on 5/25/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "SearchResultViewController.h"
#import "CommentViewController.h"
#import "CellConfig.h"
#import "BaseModel.h"
#import "ZToastManager.h"
#import "UserLoginVC.h"
#import "SearchViewController.h"
//
#import "MDSearchBarController.h"
#import "SearchTabView.h"
#import "HTHorizontalSelectionList.h"
#import "ProductImageSettingVC.h"
#import "DWTagList.h"
#import "GoodConfig.h"
#import "WebViewController.h"
@interface SearchResultViewController ()<MDSearchBarViewControllerDelegate,UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate,HTHorizontalSelectionListDataSource,HTHorizontalSelectionListDelegate,DWTagListDelegate>
{
}
@property (nonatomic,strong)NSMutableDictionary *categoryDictionary;
@property(nonatomic,strong)NSMutableArray *sectionsArray;
@property(nonatomic,strong)NSMutableArray *othersArray;
@property(nonatomic,strong)NSMutableArray *tagsArray;
@property(nonatomic,strong)NSMutableArray *brandsArray;
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong)UITextField *txtKey;
@property(nonatomic,assign)int currentPage;
@property(nonatomic,assign)BOOL isLoadFinished;
@property(nonatomic,assign)BOOL isRequesting;
@property(nonatomic,assign)BOOL isSelectedTag;

//
@property (nonatomic, strong) MDSearchBar * searchBar;
@property (nonatomic, strong) MDSearchBarController * searchBarController;
@property(nonatomic,strong) NSMutableArray *searchArray;
@property(nonatomic,strong)NSMutableArray *tabSearchArray;
@property(nonatomic,strong)SearchTabView *searchWebView;
@property(nonatomic,strong)HTHorizontalSelectionList *selectionList;
@property(nonatomic,assign)NSInteger selectionIndex;
//
@property(nonatomic,strong)UIView *menuView;
@property(nonatomic,assign)BOOL isShow;
@property (nonatomic, strong) DWTagList *tagList;
@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
     [UMengShare event:CK_SEARCHRESULT acc:1];
    self.isComment =YES;
    self.navTitle =self.keyValue;
    
    if (!_sectionsArray) {
        _sectionsArray =[[NSMutableArray alloc] init];
    }
    if (!_othersArray) {
        _othersArray =[[NSMutableArray alloc] init];
    }
    if (!_searchArray) {
        _searchArray =[[NSMutableArray alloc] init];
    }
    
    if (!_tagsArray) {
        _tagsArray =[[NSMutableArray alloc] init];
    }
    if (!_brandsArray) {
        _brandsArray =[[NSMutableArray alloc] init];
    }
    if (!_categoryDictionary) {
        _categoryDictionary =[NSMutableDictionary dictionary];
    }
    if (!_tabSearchArray) {
        _tabSearchArray=[[NSMutableArray alloc] init];
    }
    self.currentPage=0;
    _selectionIndex=0;
    self.isLoadFinished =NO;
    self.isRequesting =NO;
    self.isSelectedTag =NO;
    _isShow=NO;
    [self notificationManager];
    [self searchTab:nil];
    [self initUI];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)cleanMemoryWarning
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.categoryDictionary=nil;
    [self.sectionsArray removeAllObjects];
    self.sectionsArray=nil;
    [self.othersArray removeAllObjects];
    self.othersArray=nil;
    [self.tagsArray removeAllObjects];
    self.tagsArray=nil;
    [self.brandsArray removeAllObjects];
    self.brandsArray=nil;
  
    [self.txtKey resignFirstResponder];
    self.txtKey=nil;
    
    [self.searchArray removeAllObjects];
    self.searchArray =nil;
    self.searchBar=nil;
    self.searchBarController.delegate= nil;
    self.searchBarController.active =NO;
    self.searchBarController=nil;
    
    self.tbView.dataSource=nil;
    self.tbView.delegate=nil;
    self.tbView.tableHeaderView=nil;
    self.tbView.tableFooterView =nil;
    self.tbView =nil;
    
    [self.tabSearchArray removeAllObjects];
    self.tabSearchArray=nil;
    [self.searchWebView removeFromSuperview];
    self.searchWebView=nil;
    self.selectionList.delegate=nil;
    self.selectionList.dataSource=nil;
    [self.selectionList removeFromSuperview];
    self.selectionList=nil;
   
    [self.tagList removeFromSuperview];
    self.tagList=nil;
    
    [self.menuView removeFromSuperview];
    self.menuView=nil;
    
 
}
-(void)dealloc
{
    [self cleanMemoryWarning];

    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
            [self cleanMemoryWarning];
            [self.navigationController popViewControllerAnimated:YES];
            break;
        default:
            break;
    }
}
//////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////
-(void)searchBar:(MDSearchBarController *)searchBarController searchWithText:(NSString *)text{
    self.mkeyValue = text;
    self.navTitle =text;
    [self searchDataWithKey:text];
    //
    if (_searchWebView) {
        [_searchWebView removeFromSuperview];
        _searchWebView=nil;
    }
    self.selectionList.selectedButtonIndex=0;
//    self.tbView.tableHeaderView =nil;
    //
}
-(void)searchBar:(MDSearchBarController *)searchBarController searchWithActive:(BOOL)active
{
    [self.searchArray removeAllObjects];
    [self.searchBarController reloadData];
    
}
-(void)searchBar:(MDSearchBarController *)searchBarController searchWithKeyWord:(NSString *)isSearch
{
    self.keyValue =@"";
    self.mkeyValue = isSearch;
    self.currentPage=0;
    [self.sectionsArray removeAllObjects];
    self.searchBarController.active =NO;
    self.tbView.tableFooterView =nil;
    //
    if (_searchWebView) {
        [_searchWebView removeFromSuperview];
        _searchWebView=nil;
    }
    self.selectionList.selectedButtonIndex=0;
//    self.tbView.tableHeaderView =nil;
    //
    [_tbView reloadData];
 //   [ZShare showAnimationView:self.view center:CGPointZero];
    [self searchDataWithKey:@"" mkey:isSearch];
   
}
//
-(void)searchDataWithKey:(NSString *)key
{
    if (key==nil||key.length==0) {
        key=@"";
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:key forKey:@"word"];
    [dict setValue:@"search" forKey:@"action"];
    [dict setValue:@"" forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    __weak typeof (self) wSelf = self;
    [BaseModel suggestion:dict success:^(AFHTTPRequestOperation *operation, id result) {
        [wSelf parserSearchResultObject:result];
    } failure:^(NSError *error) {
        [wSelf.searchArray removeAllObjects];
        [wSelf.searchBarController reloadData];
    //    [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
-(void)parserSearchResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSArray class]]) {
        [self.searchArray removeAllObjects];
        self.searchArray =[NSMutableArray arrayWithArray:resultObject];
    }
    [self.searchBarController reloadData];
    
}
//////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.frame.size.width, self.view.frame.size.height-self.navHeight) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor = [UIColor whiteColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tbView];
    
    __weak typeof (self) wSelf = self;
    [_tbView addInfiniteScrollingWithActionHandler:^{
        if (wSelf.isRequesting) {
            return ;
        }
        if (wSelf.isLoadFinished) {
            //停止菊花
            [wSelf.tbView.infiniteScrollingView stopAnimating];
            [wSelf.tbView setShowsInfiniteScrolling:NO];
            return;
        }
        [wSelf.tbView.infiniteScrollingView startAnimating];
        if ([wSelf.othersArray count]>0) {
            wSelf.currentPage =(int)[wSelf.othersArray count]+1;
        }
        [wSelf searchDataWithKey:wSelf.keyValue mkey:wSelf.mkeyValue];
        
    }];
 //   [ZShare showAnimationView:self.view center:CGPointZero];
    [self searchDataWithKey:self.keyValue mkey:self.mkeyValue];
    
}

#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==_tbView) {
        return  self.sectionsArray.count;
    }else
    {
        return 1;
    }
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==_tbView)
    {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
        if ([sectionTitle isEqualToString:@"qiugou"]) {
            return 1;
        }else
        if ([sectionTitle isEqualToString:@"cate_tags"]) {
            return 1;
        }else if ([sectionTitle isEqualToString:@"cate_brands"]) {
            return 1;
        }else if ([sectionTitle isEqualToString:@"others"]) {
            return [self.othersArray count];
        }else
        {
            id tag =[self.categoryDictionary objectForKey:sectionTitle];
            if(tag && [tag isKindOfClass:[NSDictionary class]])
            {
                NSArray *keyArry =[tag allKeys];
                if ([keyArry containsObject:@"99999999"]) {
                   int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                    if (end==1) {
                       return [keyArry count]-1;
                    }
                }
                return [keyArry count];
        
            }
        }
    }else{
        return self.searchArray.count;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView==_tbView)
    {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
        
        if ([sectionTitle isEqualToString:@"cate_tags"]||[sectionTitle isEqualToString:@"cate_brands"]||[sectionTitle isEqualToString:@"qiugou"])
        {
            return PHOTO_FRAME_WIDTH;
        }if ([sectionTitle isEqualToString:@"others"]) {
            if (self.categoryDictionary && self.categoryDictionary.count>0) {
                return PHOTO_FRAME_WIDTH*5;
            }
            return 0.01;
        }
        else
        {
            return PHOTO_FRAME_WIDTH*4;
        }
        return 0.01;
    }
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView==_tbView)
    {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
        
        if ([sectionTitle isEqualToString:@"cate_tags"]||[sectionTitle isEqualToString:@"cate_brands"])
        {
            return PHOTO_FRAME_WIDTH*2;
        }
        return PHOTO_FRAME_WIDTH;
    }
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_tbView)
    {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
        if ([sectionTitle isEqualToString:@"qiugou"])
        {
            return hightAuto(75);
        }else
        if ([sectionTitle isEqualToString:@"cate_tags"]) {
            return [CategoryTagCell returnCellHeight:self.tagsArray];
        }else if ([sectionTitle isEqualToString:@"cate_brands"]) {
            return [CategoryBrandTagCell returnCellHeight:self.brandsArray];
        }else if ([sectionTitle isEqualToString:@"others"]) {
            NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
            return [CategoryListCell returnCellHeight:dict];
        }else
        {
            id tag =[self.categoryDictionary objectForKey:sectionTitle];
            if(tag && [tag isKindOfClass:[NSDictionary class]])
            {
                NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
                [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                    NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                    NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                    return [number1 compare:number2];
                }];
                if ([numArray containsObject:@"99999999"]) {
                    int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                    if (end==1) {
                        [numArray removeObject:@"99999999"];
                    }
                }
                NSString* kv =[numArray objectAtIndex:indexPath.row];
                if ([kv isEqualToString:@"99999999"]) {
                    return 60;
                }else {
                    NSDictionary* dict =[tag objectForKey:kv];
                    
                    if ([[dict objectForKey:@"landscape"] intValue]==1)
                    {
                        return [CategoryV1Cell returnCellHeight:dict];
                    }else{
                        return [CategoryListCell returnCellHeight:dict];
                    }
                    
                }
            }
            
            return 0;
        }
        
        return 0;
    }else
    {
        return 40;
    }
    return 0;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView==_tbView)
    {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
        if ([sectionTitle isEqualToString:@"cate_tags"]||[sectionTitle isEqualToString:@"cate_brands"]||[sectionTitle isEqualToString:@"qiugou"])
        {
            return nil;
        }else
            if ([sectionTitle isEqualToString:@"others"]) {
                if (self.categoryDictionary&&self.categoryDictionary.count>0) {
                    UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
                    sectionView.backgroundColor =[UIColor clearColor];
                    UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(0,PHOTO_FRAME_WIDTH+2, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*2)];
                    tagValue.backgroundColor = [UIColor clearColor];
                    tagValue.textColor = NICKNAME_COLOR;
                    tagValue.textAlignment = NSTextAlignmentCenter;
                    tagValue.font = FONT(20);
                    NSString *str =[NSString stringWithFormat:@"「 %@ 」",@"其他"];
                    tagValue.text =str;
                    [sectionView addSubview:tagValue];
                    tagValue=nil;
                    return sectionView;
                }
                return nil;
            }else
            {
                UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
                sectionView.backgroundColor =[UIColor clearColor];
                UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(0, PHOTO_FRAME_WIDTH+2, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*2)];
                tagValue.backgroundColor = [UIColor clearColor];
                tagValue.textColor = NICKNAME_COLOR;
                tagValue.textAlignment = NSTextAlignmentCenter;
                tagValue.font = FONT(20);
                NSString *str =[NSString stringWithFormat:@"「 %@ 」",sectionTitle];
                tagValue.text =str;
                [sectionView addSubview:tagValue];
                tagValue=nil;
                UIButton *sectionBtn1 =[UIButton buttonWithType:UIButtonTypeCustom];
                sectionBtn1.frame =CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*4);
                sectionBtn1.tag =section;
                sectionBtn1.backgroundColor=[UIColor clearColor];
                [sectionBtn1 addTarget:self action:@selector(sectionClicked:) forControlEvents:UIControlEventTouchUpInside];
                [sectionView addSubview:sectionBtn1];
                sectionBtn1=nil;
                return sectionView;
            }
        
        return nil;
    }
    return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = PHOTO_FRAME_WIDTH;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if (tableView==_tbView)
    {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
      
        if ([sectionTitle isEqualToString:@"cate_tags"]) {
            CategoryTagCell *cell = (CategoryTagCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[CategoryTagCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.backgroundColor=[UIColor clearColor];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            [cell showInfo:_tagsArray indexPath:indexPath];
            return cell;
        }else if ([sectionTitle isEqualToString:@"cate_brands"]) {
            CategoryBrandTagCell *cell = (CategoryBrandTagCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[CategoryBrandTagCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.backgroundColor=[UIColor clearColor];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            [cell showInfo:_brandsArray indexPath:indexPath];
            return cell;
        }else if ([sectionTitle isEqualToString:@"others"]) {
            CategoryListCell *cell = (CategoryListCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.backgroundColor=[UIColor clearColor];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            if (indexPath.row >self.othersArray.count) {
                return cell;
            }
            NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
            [cell showInfo:dict key:@"others" indexPath:indexPath];
            return cell;
        }else
        {
            id tag =[self.categoryDictionary objectForKey:sectionTitle];
            if(tag && [tag isKindOfClass:[NSDictionary class]])
            {
                NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
                [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                    NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                    NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                    return [number1 compare:number2];
                }];
                if ([numArray containsObject:@"99999999"]) {
                    int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                    if (end==1) {
                        [numArray removeObject:@"99999999"];
                    }
                }
                NSString* kv =[numArray objectAtIndex:indexPath.row];
                if ([kv isEqualToString:@"99999999"]) {
                    if(cell == nil)
                    {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                        cell.contentView.backgroundColor = [UIColor clearColor];
                        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
                        cell.selectedBackgroundView.backgroundColor =UIColorFromRGB(0xf4f4f4);
                    }
                    cell.textLabel.textAlignment =NSTextAlignmentCenter;
                    cell.textLabel.text =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end_msg"]];
                    cell.textLabel.font = FONT(16);
                    cell.textLabel.textColor=UIColorFromRGB(0x285790);
                    return cell;
                }else{
                    
                    NSDictionary* dict =[tag objectForKey:kv];
                    
                    if ([[dict objectForKey:@"landscape"] intValue]==1)
                        {
                            CategoryV1Cell *cell = (CategoryV1Cell*)[tableView  dequeueReusableCellWithIdentifier:@""];
                            if(cell == nil)
                            {
                                cell = [[CategoryV1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                                cell.backgroundColor=[UIColor clearColor];
                                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                                _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
                                __weak typeof (self) wSelf = self;
                                [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indexPath) {
                                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                                }];
                            }
                            [cell showInfo:dict other:[NSNumber numberWithBool:YES] key:sectionTitle indexPath:indexPath];
                            return cell;
                        }else{
                            CategoryListCell *cell = (CategoryListCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
                            if(cell == nil)
                            {
                                cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                                cell.backgroundColor=[UIColor clearColor];
                                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                                _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
                                __weak typeof (self) wSelf = self;
                                [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indexPath) {
                                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                                }];
                            }
                            [cell showInfo:dict key:sectionTitle indexPath:indexPath];
                            return cell;
                        }
                    }
            }
        }
        return cell;
    }else{
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
            cell.selectedBackgroundView.backgroundColor =UIColorFromRGB(0xf4f4f4);
        };
        if (indexPath.row >=self.searchArray.count) {
            return cell;
        }
        cell.textLabel.text =[NSString stringWithFormat:@"%@",[self.searchArray objectAtIndex:indexPath.row]];
        cell.textLabel.font = FONT(16);
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView==_tbView) {
        NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
        
        if ([sectionTitle isEqualToString:@"others"])
        {
            CategoryListCell *cell = (CategoryListCell *)[tableView cellForRowAtIndexPath:indexPath];
            GoodViewController *good =[[GoodViewController alloc] init];
            good.type =-1;
            good.dictionary = cell.data;
            good.photo_id = [NSString stringWithFormat:@"%@",[cell.data objectForKey:@"photo_id"]];
            [self.navigationController pushViewController:good animated:YES];
            
        }else if(![sectionTitle isEqualToString:@"search"]&&![sectionTitle isEqualToString:@"cate_tags"]&&![sectionTitle isEqualToString:@"cate_brands"])
        {
            id tag =[self.categoryDictionary objectForKey:sectionTitle];
            if(tag && [tag isKindOfClass:[NSDictionary class]])
            {
                NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
                [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                    NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                    NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                    return [number1 compare:number2];
                }];
                if ([numArray containsObject:@"99999999"]) {
                    int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                    if (end==1) {
                        [numArray removeObject:@"99999999"];
                    }
                }
                NSString* kv =[numArray objectAtIndex:indexPath.row];
                
                if ([kv isEqualToString:@"99999999"])
                {
                    NSString *cateid =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next_id"]];
                    NSString *catename =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next_name"]];
                    NSString *cate =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next"]];
                    if ([cate isEqualToString:@"cate"]) {
                        GoodClassViewController *cate =[[GoodClassViewController alloc] init];
                        cate.cateName =catename;
                        cate.cateCid =cateid;
                        cate.cateTag =@"";
                        cate.word =self.keyValue;
                        cate.mword =self.mkeyValue;
                        [self.navigationController pushViewController:cate animated:YES];
                    }else
                    {
                       [self moreWithTag:sectionTitle cat:cateid];
                    }
                    
                }else{
                    NSDictionary* dict =[tag objectForKey:kv];
                    int is_shaiDan =[[dict objectForKey:@"is_shaidan"] intValue];
                    if (is_shaiDan==20) {
                        WebViewController *web =[[WebViewController alloc] init];
                        [self.navigationController pushViewController:web animated:YES];
                        web=nil;
                        
                    }else{
                        GoodViewController *good =[[GoodViewController alloc] init];
                        good.type =-1;
                        good.dictionary = dict;
                        good.photo_id = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                        [self.navigationController pushViewController:good animated:YES];
                    }
                }
            }
            
            
        }
    }else{
        if(indexPath.row>=[self.searchArray count])
        {
            return;
        }
        NSString *key =[self.searchArray objectAtIndex:indexPath.row];
        self.keyValue =key;
        self.currentPage=0;
        [self.sectionsArray removeAllObjects];
        self.searchBarController.active =NO;
        self.tbView.tableFooterView =nil;
        [_tbView reloadData];
  //      [ZShare showAnimationView:self.view center:CGPointZero];
        [self searchDataWithKey:key mkey:self.mkeyValue];
        
    }
    
}
-(void)sectionClicked:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    NSString *key =[self.sectionsArray objectAtIndex:btn.tag];
    id tag =[self.categoryDictionary objectForKey:key];
    if(tag && [tag isKindOfClass:[NSDictionary class]])
    {
        NSString *cate =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next"]];
        if ([cate isEqualToString:@"cate"]) {
            NSString *cateid =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next_id"]];
            NSString *catename =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next_name"]];
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateName =catename;
            cate.cateCid =cateid;
            cate.cateTag =@"";
            cate.word =self.keyValue;
            cate.mword =self.mkeyValue;
            [self.navigationController pushViewController:cate animated:YES];
        }
    }
}
-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath *)indexPath
{
    
    switch (type) {
        case BEventType_category:
        {
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2"]];
            cate.cateCid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            cate.cateTag =@"";
            [self.navigationController pushViewController:cate animated:YES];
            cate=nil;
        }
            break;
        case BEventType_tagBrand:
        {
            if ([[obj objectForKey:@"name"] isEqualToString:@"更多"]) {
                return;
            }
            GoodBrandViewController *brand =[[GoodBrandViewController alloc] init];
            brand.brandName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"name"]];
            brand.brandId =[NSString stringWithFormat:@"%@",[obj objectForKey:@"id"]];
            brand.cId =@"";
            [self.navigationController pushViewController:brand animated:YES];
        }
            break;
        case BEventType_brand:
        {
            GoodBrandViewController *brand =[[GoodBrandViewController alloc] init];
            brand.brandName =[NSString stringWithFormat:@"%@", [obj objectForKey:@"brand"]];
            brand.brandId =[NSString stringWithFormat:@"%@", [obj objectForKey:@"brand_id"]];
            brand.cId =[NSString stringWithFormat:@"%@", [obj objectForKey:@"cat2_id"]];
            [self.navigationController pushViewController:brand animated:YES];
        }
            break;
        case BEventType_good:
        {
            GoodViewController *good =[[GoodViewController alloc] init];
            good.type =-1;
            good.dictionary =obj;
            good.photo_id = [NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            [self.navigationController pushViewController:good animated:YES];
        }
            break;
        case BEventType_comment:
        {
            NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            CommentViewController *comm =[[CommentViewController alloc] init];
            comm.photo_id =pid;
            comm.dictionary =obj;
            [self.navigationController pushViewController:comm animated:YES];
        }
            break;
        case BEventType_like:
        {
            if ([self isLogin]) {
                NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
                [self likeWithPid:pid key:key indexPath:indexPath];
            }
            
        }
            break;
        case BEventType_tag:
            
            break;
        case BEventType_categoryTag:
        {
            NSDictionary *dict =(NSDictionary*)obj;
            self.currentPage=0;
            self.isSelectedTag =YES;
            int selected =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"selected"]] intValue];
            if (selected==0) {
                [self searchDataWithKey:self.keyValue mkey:self.mkeyValue tag:[NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]] cateid:[NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]]];
            }else
            {
                [self searchDataWithKey:self.keyValue mkey:self.mkeyValue tag:@"" cateid:@""];
            }
            
        }
            break;
        case BEventType_search:
        {
            self.currentPage=0;
        }
            break;
        
        default:
            break;
    }
}
-(void)searchDataWithKey:(NSString *)key mkey:(NSString*)mkey cateid:(NSString*)cateid{
    [self searchDataWithKey:key mkey:mkey tag:@"" cateid:cateid];
}
-(void)searchDataWithKey:(NSString *)key mkey:(NSString*)mkey{
    [self searchDataWithKey:key mkey:mkey tag:@"" cateid:@""];
}
-(void)searchDataWithKey:(NSString *)key mkey:(NSString*)mkey tag:(NSString*)tag cateid:(NSString*)cateid
{
    if (key.length>0) {
        self.searchBarController.searchBarView.textField.text = key;
    }else{
        self.searchBarController.searchBarView.textField.text = mkey;
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    
    [dict setValue:key forKey:@"word"];
    [dict setValue:mkey forKey:@"mword"];
    [dict setValue:tag forKey:@"tag"];
    [dict setValue:cateid forKey:@"cat2_id"];
    [dict setValue:@"0" forKey:@"from"];
    __weak typeof (self) wSelf = self;
    self.isRequesting =YES;
    [BaseModel moresearch:dict success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        [wSelf parserResultObject:result];
        wSelf.isRequesting =NO;
        [wSelf.tbView.infiniteScrollingView stopAnimating];
    } failure:^(NSError *error) {
          wSelf.isRequesting =NO;
         [wSelf.tbView.infiniteScrollingView stopAnimating];
    //     [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    [[ZToastManager ShardInstance] hideprogress];
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        //
        if (self.currentPage==0) {
            [self.sectionsArray removeAllObjects];
            self.sectionsArray =[NSMutableArray arrayWithArray:@[@"cate_tags",@"cate_brands",@"others"]];
            //
             NSMutableArray *sortTempArray =[NSMutableArray array];
            id cate_tags =  [resultObject objectForKey:@"cate_tags"];
            if (cate_tags && [cate_tags isKindOfClass:[NSArray class]]){
                [_tagsArray removeAllObjects];
                for (NSDictionary *di in cate_tags) {
                    if (di && [di isKindOfClass:[NSDictionary class]])
                    {
                        [self.tagsArray addObject:di];
                        [sortTempArray addObject:[di objectForKey:@"name"]];
                    }
                }
                if ([self.tagsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_tags"];
                }
            }else
            {
                if (self.currentPage==0 || [self.tagsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_tags"];
                }
            }
            //
            id cate_brands =  [resultObject objectForKey:@"cate_brands"];
            if (cate_brands && [cate_brands isKindOfClass:[NSArray class]]){
                [_brandsArray removeAllObjects];
                for (NSDictionary *di in cate_brands) {
                    if (di && [di isKindOfClass:[NSDictionary class]])
                    {
                        [self.brandsArray addObject:di];
                    }
                }
                if ([self.brandsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_brands"];
                }
            }else
            {
                if (self.currentPage==0 || [self.brandsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_brands"];
                }
            }
            
            //
            id data =[resultObject objectForKey:@"data"];
            if (data && [data isKindOfClass:[NSDictionary class]]) {
                
                int scount =(int)[sortTempArray count];
                if(scount>0)
                {
                    for (int i =0 ;i<scount ;i++) {
                        NSString *tagName =[sortTempArray objectAtIndex:i];
                        for (NSString *datakey in [data allKeys])
                        {
                            if ([datakey isEqualToString:tagName]) {
                                [self.sectionsArray insertObject:datakey atIndex:[self.sectionsArray count]-1];
                                break;
                            }
                        }
                    }
                }else{
                    for (NSString *datakey in [data allKeys])
                    {
                        [self.sectionsArray insertObject:datakey atIndex:[self.sectionsArray count]-1];
                    }
                }
                [self.categoryDictionary addEntriesFromDictionary:data];
            }else
            {
                [self.categoryDictionary removeAllObjects];
            }
            //
            id others =[resultObject objectForKey:@"others"];
            
            if (others && [others isKindOfClass:[NSArray class]]) {
                if (self.currentPage==0) {
                    [self.othersArray removeAllObjects];
                }
                for (NSDictionary *dict in others) {
                    [self.othersArray addObject:dict];
                }
            }else
            {
                if (self.currentPage==0|| [self.othersArray count]==0) {
                    [self.sectionsArray removeObject:@"others"];
                }
            }
            
            
        }else
        {
            id others =[resultObject objectForKey:@"others"];
            
            if (others && [others isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dict in others) {
                    [self.othersArray addObject:dict];
                }
            }
        }
        //
        id tabSearch =[resultObject objectForKey:@"search_tab"];
        if (tabSearch && [tabSearch isKindOfClass:[NSArray class]]) {
            [self.tabSearchArray removeAllObjects];
            for (id dict in tabSearch) {
                [self.tabSearchArray addObject:dict];
            }
            [self searchTab:self.tabSearchArray];
        }else{
            [self searchTab:nil];
        }
        //end
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
       
        if (self.isLoadFinished) {
            [self.tbView.infiniteScrollingView stopAnimating];
            [self.tbView setShowsInfiniteScrolling:NO];
            NSString *end_msg =[resultObject objectForKey:@"end_msg"];
            self.tbView.tableFooterView =[ZShare footerView:end_msg];
        }
        id no_data =  [resultObject objectForKey:@"no_data"];
        if (no_data&&[no_data isKindOfClass:[NSString class]]) {
   //         [ZShare hiddenAnimationView:self.view text:no_data];
            self.tbView.tableFooterView =[ZShare footerView:no_data];
        }else {
     //        [ZShare hiddenAnimationView:self.view];
        }
        [_tbView reloadData];
    }
}
//更多
-(void)moreWithTag:(NSString *)tagstr cat:(NSString *)cat
{
    if (tagstr==nil||tagstr.length==0) {
        return;
    }
    NSDictionary *dictk =[self.categoryDictionary objectForKey:tagstr];
    int from =(int)[[dictk allKeys] count]-1;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:self.keyValue forKey:@"word"];
    [dict setValue:self.mkeyValue forKey:@"mword"];
    [dict setValue:tagstr forKey:@"tag"];
    [dict setValue:cat forKey:@"cat2_id"];
    [dict setValue:[NSString stringWithFormat:@"%d",from] forKey:@"from"];
    __weak typeof (self) wSelf = self;
    self.isRequesting =YES;
    [BaseModel moresearch:dict success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        [wSelf parserMoreObject:result tag:tagstr];
        wSelf.isRequesting =NO;
        [wSelf.tbView.infiniteScrollingView stopAnimating];
  //      [ZShare hiddenAnimationView:wSelf.view];
    } failure:^(NSError *error) {
        wSelf.isRequesting =NO;
        [wSelf.tbView.infiniteScrollingView stopAnimating];
   //     [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}
-(void)parserMoreObject:(id)resultObject tag:(NSString*)tag
{
    if (resultObject==nil) {
        return;
    }
    id more =[resultObject objectForKey:@"data"];
    if (more && [more isKindOfClass:[NSDictionary class]]) {
        NSArray *keyk =[more allKeys];
        if ([keyk count]>0) {
            if ([keyk containsObject:tag]) {
                NSMutableDictionary *dictOld = [[self.categoryDictionary objectForKey:tag] mutableCopy];
                NSDictionary* d = [more objectForKey:tag];
                [d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                    [dictOld setValue:obj forKey:key];
                }];
                [self.categoryDictionary setValue:dictOld forKey:tag];
            }
        }
    }
    [_tbView reloadData];
}

//喜欢
-(void)likeWithPid:(NSString*)pid key:(NSString*)key indexPath:(NSIndexPath*)indexPath
{
    [UMengShare event:CK_LOVE acc:1];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:pid forKey:@"photo_id"];
    [BaseModel love:dict success:^(AFHTTPRequestOperation *operation, id result) {
        int code =  [[result objectForKey:@"code"] intValue];
        NSString *like_cnt =  [NSString stringWithFormat:@"%@",[result objectForKey:@"like_cnt"]];
        if (code==1 || code==2)
        {
            if ([key isEqualToString:@"others"]) {
                for (int i=0; i<[wSelf.othersArray count]; i++) {
                    NSDictionary *dict =[wSelf.othersArray objectAtIndex:i];
                    if ([dict isKindOfClass:[NSDictionary class]]) {
                        NSString *pidd =[NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                        if ([pidd isEqualToString:pid]) {
                            NSMutableDictionary *temp = [dict mutableCopy];
                            if (code==1) {
                                [temp setValue:@"1" forKey:@"like"];
                            }else{
                                [temp setValue:@"0" forKey:@"like"];
                            }
                            [temp setValue:like_cnt forKey:@"like_cnt"];
                            [wSelf.othersArray replaceObjectAtIndex:i withObject:temp];
                            break;
                        }
                    }
                }
            }else{
                [wSelf.categoryDictionary enumerateKeysAndObjectsUsingBlock:^(id key,id value,BOOL *stop){
                    id keyArray = [[wSelf.categoryDictionary objectForKey:key] mutableCopy];
                    if(keyArray && [keyArray isKindOfClass:[NSDictionary class]])
                    {
                        NSArray *keyArry =[keyArray allKeys];
                        keyArry= [keyArry sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
                        NSMutableArray *key1 =[NSMutableArray arrayWithArray:keyArry];
                        if ([key1 containsObject:@"99999999"]) {
                            [key1 removeObject:@"99999999"];
                        }
                        
                        for(int i =0 ;i<[key1 count];i++)
                        {
                            NSString *kv =[key1 objectAtIndex:i];
                            NSDictionary *dict =[keyArray objectForKey:kv];
                            NSString *pidd =[NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                            if ([pidd isEqualToString:pid]) {
                                NSMutableDictionary *temp = [dict mutableCopy];
                                if (code==1) {
                                    [temp setValue:@"1" forKey:@"like"];
                                }else{
                                    [temp setValue:@"0" forKey:@"like"];
                                }
                                [temp setValue:like_cnt forKey:@"like_cnt"];
                                [keyArray setValue:temp forKey:kv];
                                
                                break;
                            }
                        }
                    }
                    [self.categoryDictionary setValue:keyArray forKey:key];
                }];
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:nil];
            [_tbView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }
        
    } failure:^(NSError *error) {
    //    [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}

-(void)notificationManager
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateLoveList:)
                                                 name:@"updateLoveList"
                                               object:nil];
}
-(void)updateLoveList:(NSNotification*)notification
{
    DLog(@"%@",notification.object);
    self.currentPage = 0;
    [self searchDataWithKey:self.keyValue mkey:self.mkeyValue];
}
//
-(void)searchTab:(NSMutableArray*)array
{
    CGFloat hight = 54;
    if (array==nil || array.count>0) {
        hight =98;
    }
    UIView *headView =[[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, 0, _tbView.frame.size.width-PHOTO_FRAME_WIDTH*2,hight)];
    headView.backgroundColor=[UIColor clearColor];
    self.tbView.tableHeaderView =headView;
    
    if (self.searchBar) {
        [self.searchBar removeFromSuperview];
        self.searchBar=nil;
    }
    self.searchBar =[[MDSearchBar alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH, 54)];
    [headView addSubview:self.searchBar];
    
    if (self.searchBarController) {
        self.searchBarController.delegate= nil;
        self.searchBarController.active =NO;
        self.searchBarController=nil;
    }
    self.searchBarController = [[MDSearchBarController alloc] init];
    self.searchBarController.delegate= self;
    self.searchBarController.searchBarView.frame = self.searchBar.frame;
    [self.searchBar.superview addSubview:self.searchBarController.searchBarView];
    [self.searchBar removeFromSuperview];
    self.searchBar = self.searchBarController.searchBarView;
    
    //
    if (_selectionList) {
        _selectionList.delegate=nil;
        _selectionList.dataSource=nil;
        [_selectionList removeFromSuperview];
        _selectionList=nil;
    }
    self.selectionList = [[HTHorizontalSelectionList alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, 54, headView.frame.size.width-PHOTO_FRAME_WIDTH*2, 44)];
    self.selectionList.delegate = self;
    self.selectionList.dataSource = self;
    self.selectionList.selectionIndicatorColor=UIColorFromRGB(0xd21d1d);
    self.selectionList.bottomTrimColor =UIColorFromRGB(0xc7c8c8);
    [headView addSubview:self.selectionList];
    //
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:@"" forKey:@"key"];
    [dict setValue:@"单品" forKey:@"name"];
    [dict setValue:@"" forKey:@"url"];
    [self.tabSearchArray insertObject:dict atIndex:0];
    //
    [self.selectionList setSelectedButtonIndex:0];
    
    UIButton *menuBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.backgroundColor=[UIColor clearColor];
    menuBtn.frame =CGRectMake(headView.frame.size.width-widthAuto(60)-PHOTO_TIME_PADDING,54+(44-hightAuto(32))/2, widthAuto(60), hightAuto(32));
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(showMenuView) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:menuBtn];
    
}

#pragma mark - HTHorizontalSelectionListDataSource Protocol Methods

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.tabSearchArray.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    if (index>=self.tabSearchArray.count) {
        return @"";
    }
    NSDictionary *dict =[self.tabSearchArray objectAtIndex:index];
    NSString *kstr =[dict objectForKey:@"name"];
    return kstr;
}
#pragma mark - HTHorizontalSelectionListDelegate Protocol Methods

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    _selectionIndex=index;
    if (index==0) {
        if (_searchWebView) {
            [_searchWebView removeFromSuperview];
            _searchWebView=nil;
        }

        return;
        
    }
    if (index>=self.tabSearchArray.count) {
        return;
    }

    NSDictionary *dict =[self.tabSearchArray objectAtIndex:index];
    NSString *kstr =[dict objectForKey:@"url"];
    NSString *key =[dict objectForKey:@"key"];
    [self webViewSearchWithUrl:kstr key:key];
}

-(void)webViewSearchWithUrl:(NSString*)urlstr key:(NSString*)key
{
    if (_searchWebView) {
        [_searchWebView removeFromSuperview];
        _searchWebView=nil;
    }
    self.searchWebView = [[SearchTabView alloc] initWithFrame:CGRectMake(0, self.navHeight+98, self.view.frame.size.width, self.view.frame.size.height-(self.navHeight+88))];
    __weak typeof (self) wSelf = self;
    [self.searchWebView setSelectedCallBack:^(id key, id data,id str) {
        [wSelf fetchItemData:data key:key str:str];
    }];
    [self.view addSubview:self.searchWebView];                     //连接到一个现有的窗口上
    [self.searchWebView loadRequestUrl:urlstr key:key];
}
-(void)fetchItemData:(NSString*)data key:(NSString*)key str:(NSString*)str
{
    if ([str isEqualToString:@"back"]) {
        [self cleanMemoryWarning];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        NSMutableDictionary *dict =[NSMutableDictionary dictionary];
        [dict setValue:data forKey:@"data"];
        [dict setValue:key forKey:@"key"];
        __weak typeof (self) wSelf = self;
        [[ZToastManager ShardInstance] showprogress];
        [BaseModel postItem:dict success:^(AFHTTPRequestOperation *operation, id result) {
            [wSelf parserMoreObject:result key:key];
        } failure:^(NSError *error) {
            [[ZToastManager ShardInstance] hideprogress];
        }];
    }
    
}
-(void)parserMoreObject:(id)resultObject key:(NSString*)key
{
    if (resultObject==nil) {
        [[ZToastManager ShardInstance] hideprogress];
        return;
    }
    int code =[[resultObject objectForKey:@"code"] intValue];
    if (code<1) {
        [[ZToastManager ShardInstance] hideprogress];
        [[ZToastManager ShardInstance] showtoast:[resultObject objectForKey:@"msg"]];
        
        return;
    }else if (code==1)
    {
        [[ZToastManager ShardInstance] hideprogress];
        id data =[resultObject objectForKey:@"data"];
        if (data&&[data isKindOfClass:[NSDictionary class]]) {
            ProductImageSettingVC *pimage =[[ProductImageSettingVC alloc] init];
            pimage.dictionary =data;
            pimage.type=3;
            [self.navigationController pushViewController:pimage animated:YES];
            pimage=nil;
        }
        
    }else
    {
        NSString *urlstr =[NSString stringWithFormat:@"%@",[resultObject objectForKey:@"url"]];
        [self scrapywithUrl:urlstr key:key];
    }
    
}

-(void)scrapywithUrl:(NSString*)str key:(NSString*)key
{
    if(str.length==0||[str isEqualToString:@"(null)"])
    {
        [[ZToastManager ShardInstance] hideprogress];
        return;
    }
    NSString *data = [NSString stringWithContentsOfURL:[NSURL URLWithString:str] encoding:NSASCIIStringEncoding error:nil];
    [self fetchItemData:data key:key str:@"recommend"];
}

-(void)showMenuView
{
    if (_isShow) {
        [UIView animateWithDuration:0.03 animations:^{
            //
            self.menuView.frame = CGRectMake(0, self.navHeight+54, self.view.frame.size.width, 0);
        }completion:^(BOOL finished) {
            //
             self.tbView.scrollEnabled = YES;
            [self.menuView removeFromSuperview];
            self.menuView=nil;
            _isShow =NO;
        }];
        
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            //
            self.menuView.frame = CGRectMake(0, self.navHeight+54, self.view.frame.size.width,self.menuView.frame.size.height);
        }completion:^(BOOL finished) {
            //
             self.tbView.scrollEnabled = NO;
            self.menuView.backgroundColor=[UIColor whiteColor];
            [self.view addSubview:self.menuView];
            _isShow =YES;
        }];
    }
}
-(UIView*)menuView
{
    if (!_menuView) {
        CGSize size =[self.tagList fittedSize];
        self.menuView=[[UIView alloc] initWithFrame:CGRectMake(0, self.navHeight+54, self.view.frame.size.width, size.height+64)];
        self.menuView.backgroundColor=[UIColor whiteColor];
        self.menuView.tag=1000;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, 12, self.menuView.frame.size.width-PHOTO_FRAME_WIDTH,20)];
        [titleLabel setBackgroundColor:[UIColor whiteColor]];
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        [titleLabel setFont:FONT(15)];
        [titleLabel setTextColor:UIColorFromRGB(0x999999)];
        titleLabel.text =@"选择搜索平台";
        [self.menuView addSubview:titleLabel];
        
        UIButton *menuBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        menuBtn.backgroundColor=[UIColor whiteColor];
        menuBtn.frame =CGRectMake(self.menuView.frame.size.width-PHOTO_TIME_PADDING-widthAuto(23),(44-hightAuto(11))/2, widthAuto(23), hightAuto(11));
        [menuBtn setBackgroundImage:[UIImage imageNamed:@"arrow_up.png"] forState:UIControlStateNormal];
        [menuBtn addTarget:self action:@selector(showMenuView) forControlEvents:UIControlEventTouchUpInside];
        [self.menuView addSubview:menuBtn];
        
        UIView *line =[[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, 44, self.menuView.frame.size.width-PHOTO_FRAME_WIDTH*2, 0.5)];
        line.backgroundColor=UIColorFromRGB(0xcccccc);
        [self.menuView addSubview:line];
        
        self.tagList.frame =CGRectMake(PHOTO_FRAME_WIDTH, 60, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, size.height);
        [self.menuView addSubview:self.tagList];
        //
        UIView *line2 =[[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, self.menuView.frame.size.height, self.menuView.frame.size.width-PHOTO_FRAME_WIDTH*2, 0.5)];
        line2.backgroundColor=UIColorFromRGB(0xcccccc);
        [self.menuView addSubview:line2];

    }
    _menuView.backgroundColor = [UIColor whiteColor];
    return _menuView;
}
-(DWTagList*)tagList
{
    if (!_tagList) {
        _tagList = [[DWTagList alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 50.0f)];
        _tagList.backgroundColor =[UIColor whiteColor];
        [_tagList setAutomaticResize:YES];
        [_tagList setTextColor:[UIColor blackColor]];
        [_tagList setTagDelegate:self];
        [_tagList setCornerRadius:4.0f];
        [_tagList setBorderColor:UIColorFromRGB(0xb5b5b5)];
        [_tagList setBorderWidth:0.5f];
        [_tagList setHighlightedBackgroundColor:UIColorFromRGB(0xf4f4f4)];
        NSMutableArray *array =[NSMutableArray array];
        for (id key in self.tabSearchArray) {
            if (key && [key isKindOfClass:[NSDictionary class]]) {
                NSString *name =[key objectForKey:@"name"];
                [array addObject:name];
            }else if (key && [key isKindOfClass:[NSString class]])
            {
                [array addObject:key];
            }
        }
        [_tagList setTags:array];
    }
    return _tagList;
}
- (void)selectedTag:(NSString *)tagName tagIndex:(NSInteger)tagIndex
{
    [UIView animateWithDuration:0.03 animations:^{
        //
        self.menuView.frame = CGRectMake(0, self.navHeight, self.view.frame.size.width, 0);
    }completion:^(BOOL finished) {
        //
        self.tbView.scrollEnabled = YES;
        [self.menuView removeFromSuperview];
        self.menuView=nil;
        _isShow =NO;
    }];
    [self.selectionList setSelectedButtonIndex:tagIndex];
    [self.selectionList reloadData];
    [self selectionList:self.selectionList didSelectButtonWithIndex:tagIndex];
}
@end
